package CheckProblem;

import com.fasterxml.jackson.databind.ObjectMapper;
import net.sf.json.JSONObject;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by wangyg 
 * on 2017/7/6 17:17
 */
public class Task {

	public static String[][] newValue = { { "235010004", "8100000" }, { "381000960", "8100000" } };

	private static ObjectMapper mapper = new ObjectMapper();

	public static void main(String[] args) throws IOException {
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
						   @Override
						   public void run() {

							   try {
								   WriteConstantHelper();
							   } catch (NoSuchFieldException e) {
								   e.printStackTrace();
							   } catch (IllegalAccessException e) {
								   e.printStackTrace();
							   } catch (IOException e) {
								   e.printStackTrace();
							   }
							   try {
								   System.out.println(buildCacheObject(Constant.PASSED_SERV_AND_AREA));
							   } catch (IOException e) {
								   e.printStackTrace();
							   }
						   }
					   }

				, 5000, 5 * 1000);


	}

	public static void WriteConstantHelper() throws NoSuchFieldException, IllegalAccessException, IOException {
		Constant c = new Constant();
		Field field = Constant.class.getDeclaredField("PASSED_SERV_AND_AREA");
		Field modifiers = field.getClass().getDeclaredField("modifiers");
		modifiers.setAccessible(true);
		modifiers.setInt(field, field.getModifiers() - Modifier.FINAL);
		field.setAccessible(true);
		field.set(c, newValue);

//		System.out.println(buildCacheObject(Constant.PASSED_SERV_AND_AREA));

	}

	public static void setValue() throws NoSuchFieldException {
		Field targetField = Constant.class.getDeclaredField("PASSED_SERV_AND_AREA");
		int modBak = targetField.getModifiers();
		Field modifiersField = null;
		try {
			targetField.setAccessible(true);
			modifiersField = Field.class.getDeclaredField("modifiers");
			modifiersField.setAccessible(true);
			if (Modifier.isFinal(targetField.getModifiers())) {
				//去掉final修饰符
				modifiersField.setInt(targetField, targetField.getModifiers() - Modifier.FINAL);
			}
			targetField.set(null, newValue);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (targetField.getModifiers() != modBak && modifiersField != null) {
				//恢复去掉的final修饰符
				try {
					modifiersField.setInt(targetField, modBak);
				} catch (IllegalArgumentException e) {
				} catch (IllegalAccessException e) {
				}
			}
		}
	}




	protected static String buildCacheObject(Object object) throws IOException {
		CacheObject co = new CacheObject();
		co.setObj(object);
		if (object instanceof Collection) {
			Collection col = (Collection) object;
			if (col.size() > 0) {
				co.setClz(col.iterator().next().getClass().getName());
			} else {
				co.setClz("null");
			}
		} else {
			co.setClz(object.getClass().getName());
		}
		//value = JsonConverter.buildNormalConverter().toJson(co);
		return mapper.writeValueAsString(co);
	}
}
